/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.LocumDao;
import cc.altius.model.Locum;
import java.util.List;

import javax.sql.DataSource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author sagar
 */
@Repository("LocumDao")
public class LocumDaoImpl implements LocumDao {

    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    @Autowired
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    Session session;
    Query query;
    

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public int addLocum(Locum locum) {
        return (Integer) this.hibernateTemplate.save(locum);
    }

    @Override
    @Transactional
    public int updateLocum(Locum locum, int locumId) {
       session = this.sessionFactory.getCurrentSession();
       String hql = "Update "
                + " Locum as locum "
                + " set "
                + " locum.locumName=:locumName,"
                + " locum.contact1=:contact1,"
                + " locum.zoneId.zoneId=:zoneId,"
                + " locum.contact2=:contact2,"
                + "locum.timing=:timing,"
                + " locum.address=:address, "
                + " locum.locumType=:locumType, "
                + " locum.latitude=:latitude, "
                + " locum.longitude=:longitude"
                + " WHERE locum.locumId=:locumId";
        query = session.createQuery(hql);
        query.setParameter("locumName", locum.getLocumName());
        query.setParameter("contact1", locum.getContact1());
        query.setParameter("zoneId", locum.getZoneId().getZoneId());
        query.setParameter("contact2",locum.getContact2());
        query.setParameter("timing", locum.getTiming());
        query.setParameter("address", locum.getAddress());
        query.setParameter("locumType", locum.getLocumType());
        query.setParameter("latitude", locum.getLatitude());
        query.setParameter("longitude", locum.getLongitude());
        query.setParameter("locumId", locum.getLocumId());
        int rowCount = query.executeUpdate();
        System.out.println("rowcount" + rowCount);
        return rowCount;
    }

    @Override
    @Transactional
    public List<Locum> getLocumList() {
        List<Locum> locumList = this.hibernateTemplate.find("from Locum");
        System.out.println("locum List:"+locumList);
        return locumList;
    }

    @Override
    @Transactional
    public Locum getLocumById(int locumId) {
        session = this.sessionFactory.getCurrentSession();
        query = session.createQuery("from Locum where locumId=" + locumId);
        return (Locum) query.uniqueResult();
    }
}
